﻿using System;
using System.ComponentModel.DataAnnotations;
using easyVal.ServerSide;

namespace easyVal.ValidationAttributes
{
    public class USDollarAttribute : ValidationAttribute, IClientValidationMethod
    {
        public bool DollarSignAllowedInClent { get; set; }
        public bool WholeNumber { get; set; }
        public bool Positive { get; set; }

        #region IClientValidationMethod Members

        public string ClientMethodName
        {
            get { return "USdollar"; }
        }

        public string ClientParameters
        {
            get
            {
                return string.Format("[{0},{1},{2}]",
                                     DollarSignAllowedInClent ? "1" : "0",
                                     WholeNumber ? "1" : "0",
                                     Positive ? "1" : "0");
            }
        }

        #endregion

        public override bool IsValid(object value)
        {
            if (ValidationAttributeUtil.IsDefaultValue(value))
                return true;

            if (!(value is decimal))
                return false;

            var decimalValue = (decimal) value;

            if (Positive && decimalValue < 0)
                return false;

            if (WholeNumber && (decimalValue != Math.Floor(decimalValue))) //Todo: test
                return false;

            return true;
        }
    }
}